package com.yyk.yykywd.mvvm.ui

import android.view.KeyEvent
import android.view.View
import androidx.lifecycle.Observer
import com.orhanobut.logger.Logger
import com.yyk.yykywd.*
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.mvvm.base.LoadRegionData
import com.yyk.yykywd.mvvm.view_model.AddCustomerViewModel
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_add_agent_customer.*
import kotlinx.android.synthetic.main.rl_bottom1.*

class AddAgentCustomerActivity : LoadRegionData() {
    private lateinit var viewModel: AddCustomerViewModel

    override fun initData() {
        viewModel.getType()
        //加载省市级选择器
        mHandler.sendEmptyMessage(MSG_LOAD_DATA)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_add_agent_customer
    }

    override fun initView() {
        viewModel = getViewModel(this, AddCustomerViewModel::class.java)
        setBackArrow {
            if (judge()) {
                backLogic(mActivity) {
                    finish()
                }
            } else {
                finish()
            }
        }
        setTitle("新增代理商客户")
        tv_founder.text = "业务创建人：" + getName()
        tv_department.text = getDepartment()
    }

    override fun setListener() {
        super.setListener()

        viewModel.base.observe(this, Observer<BaseBean> {
            if (it.code == 306) {
                showAlreadyAdd(mActivity, AGENT)
                return@Observer
            }
            if (it.code == 200) {
                ToastUtil.showShort("新增成功")
                finish()
            }

        })

        tv_save.setOnClickListener {
            if (required()) {
                return@setOnClickListener
            }
            newCustomer(mActivity) {
                addCustomer("1")
            }

        }
        tv_add.setOnClickListener {
            if (required()) {
                return@setOnClickListener
            }
            addSeas(mActivity) {
                addCustomer("3")
            }

        }

        tv_agent_region.setOnClickListener {
            if (isLoaded) {
                showPickerView()
            }
        }

        tv_agent_type.setOnClickListener {
            val showOptionsPicker = showOptionsPicker(mActivity) { options1: Int, _: Int, _: Int ->
                viewModel.selectedTypePosition = options1
                tv_agent_type.text = viewModel.agentType[options1]
            }
            showOptionsPicker.setSelectOptions(0, 0, 0)
            showOptionsPicker.setPicker(viewModel.agentType) //三级选择器
            showOptionsPicker.show()
        }

        tv_agent_source.setOnClickListener {
            val showOptionsPicker = showOptionsPicker(mActivity) { options1: Int, _: Int, _: Int ->
                tv_agent_source.text = viewModel.agentSource[options1]
                if (getTextString(tv_agent_source) == "其他") {
                    cl_source.visibility = View.VISIBLE
                } else {
                    cl_source.visibility = View.GONE
                }
            }
            showOptionsPicker.setSelectOptions(0, 0, 0)
            showOptionsPicker.setPicker(viewModel.agentSource) //三级选择器
            showOptionsPicker.show()
        }
    }

    private fun addCustomer(flag: String) {
        var source = ""
        source = if (getTextString(tv_agent_source) == "其他") {
            getTextString(et_source)
        } else {
            getTextString(tv_agent_source)
        }
        viewModel.addAgentClient(viewModel.agentTypeId[viewModel.selectedTypePosition],
                getTextString(et_agent_name),
                getTextString(et_contact),
                getTextString(tv_agent_region),
                source,
                getTextString(et_agent_industry),
                getTextString(et_agent_describe),
                flag)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (judge()) {
                backLogic(mActivity) {
                    finish()
                }
            } else {
                finish()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    private fun judge(): Boolean {
        if (getTextString(tv_agent_type) != "请选择") {
            return true
        }
        if (getTextString(et_agent_name).isNotBlank()) {
            return true
        }
        if (getTextString(et_contact).isNotBlank()) {
            return true
        }
        if (getTextString(tv_agent_region) != "请选择") {
            return true
        }
        if (getTextString(tv_agent_source) != "请选择") {
            return true
        }
        if (getTextString(et_agent_industry).isNotBlank()) {
            return true
        }
        if (getTextString(et_agent_describe).isNotBlank()) {
            return true
        }
        return false
    }

    private fun required(): Boolean {
        if (getTextString(tv_agent_type) == "请选择") {
            ToastUtil.showShort("请选择代理商类型")
            return true
        }
        if (getTextString(et_agent_name).isBlank()) {
            ToastUtil.showShort("请填写代理商姓名")
            return true
        }
        if (getTextString(et_contact).isBlank()) {
            ToastUtil.showShort("请填写联系方式")
            return true
        }
        if (getTextString(tv_agent_region) == "请选择") {
            ToastUtil.showShort("请选择代理商地区")
            return true
        }
        if (getTextString(tv_agent_source) == "请选择") {
            ToastUtil.showShort("请选择代理商来源")
            return true
        }
        if (getTextString(tv_agent_source) == "其他" && getTextString(et_source).isBlank()){
            ToastUtil.showShort("请填写代理商来源")
            return true
        }
        return false
    }

    private fun showPickerView() { // 弹出选择器
        val pvOptions = showOptionsPicker(mActivity) { options1: Int, options2: Int, options3: Int ->
            val tx = options1Items[options1].pickerViewText.toString() + "—" +
                    options2Items[options1][options2]
            tv_agent_region.text = tx
            province_id = index1Items[options1].name_id
            city_id = index2Items[options1][options2]
            county_id = index3Items[options1][options2][options3]
            Logger.e("省市区$province_id,$city_id,$county_id")
        }
        pvOptions.setSelectOptions(0, 0, 0)
//        pvOptions.setPicker(options1Items as List<Any>, options2Items as List<MutableList<Any>>?, options3Items as List<MutableList<MutableList<Any>>>) //三级选择器
        pvOptions.setPicker(options1Items as List<Any>, options2Items as List<MutableList<Any>>?, null) //三级选择器
        pvOptions.show()
    }
}
